<?php

/**
 * this class provides functions for creating an jquery easyui tree
 * @author		ungerboeck simon <su@militaerversand.at>
 *
 */
class Tree {
	private $id = null;
	private $checkbox = null;
	private $treeNodes = null;
	private $isSubTree = null;
	private $nodeCheckFunk = null;
	

	/************************************************************************
	 ************************** PUBLIC FUNCTIONS ****************************
	 ************************************************************************/
	
	/**
	 * initializes the member variables
	 * @param 		string		 $id
	 */
	public function __construct($id = null){
		$this->id = $id;
		$this->treeNodes = array();
		$this->isSubTree = false;
	}
	
	
	/**
	 * enalbes checkboxes in the tree
	 * @param 		boolean		 $checkbox
	 */
	public function enableCheckboxes($checkbox = true){
		$this->checkbox = $checkbox;
	}
	
	
	/**
	 * adds a new tree node to the tree
	 * @param 		mixed		 $node
	 */
	public function addNode(TreeNode $node){
		if ($node != null)
			array_push($this->treeNodes, $node);
	}
	
	
	/**
	 * creates the tree with all tree nodes
	 * @return		string		$output
	 */
	public function create(){
		$output = '<ul ';
		if (!$this->isSubTree){
			$output .= (($this->id != null) ? 'id="' . $this->id . '" ' : '');
			$output .= 'class="easyui-tree" ';
		}
		$output .= (($this->checkbox) ? 'checkbox="true" cascadeCheck="true" ' : '');
		$output .= 'animate="true" ';
		$output .= 'data-options="' . ((isset($this->nodeCheckFunk)) ? 'onCheck:' . $this->nodeCheckFunk : '') . '"';
		$output .= '>';
		
		foreach($this->treeNodes as $treeNode)
			$output .= $treeNode->create();
		
		$output .= '</ul>';
		return $output;
	}
	
	
	/**
	 * returns the created tree
	 * @return 		string		
	 */
	public function __toString(){
		return $this->create();
	}


	/************************************************************************
	 **************************** GETTER / SETTER ***************************
	 ************************************************************************/
	
	public function setId($id){
		$this->id = $id;
	}
	

	public function setIsSubTree($isSubTree){
		$this->isSubTree = $isSubTree;
	}
	
	
	public function setNodeCheckFunc($function){
		$this->nodeCheckFunk = $function;
	}
	
	
	public function getId(){
		return $this->id;
	}
	
	
	public function isSubTree(){
		return $this->isSubTree;
	}
	
	
	public function getNodeCheckFunc(){
		return $this->nodeCheckFunk;
	}
	
	
	
	



	/************************************************************************
	 ************************** PRIVATE FUNCTIONS ***************************
	 ************************************************************************/

}


?>